package com.zhongli.devplatform.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.regexp.internal.RE;
import com.zhongli.devplatform.bean.Res;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.po.common.ReadCommentLikePO;
import com.zhongli.devplatform.po.common.ReadReplyLikePO;
import com.zhongli.devplatform.service.imp.common.ReadCommentLikeService;
import com.zhongli.devplatform.service.imp.common.ReadReplyLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author wy
 * @since 2020/8/29
 */
@Api(tags = "评论管理", description = "评论管理", value = "评论管理")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ReadCommentLikeService commentLikeService;

    @Autowired
    private ReadReplyLikeService replyLikeService;

    @ApiOperation("点赞评论")
    @PostMapping("/likeCommon/{commonId}")
    public Res<String> likeCommon(@PathVariable Integer commonId){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        QueryWrapper<ReadCommentLikePO> wrapper = new QueryWrapper<ReadCommentLikePO>()
                .eq("comment_id", commonId)
                .eq("user_id", currentUserId);
        ReadCommentLikePO one = commentLikeService.getOne(wrapper);
        if (Objects.isNull(one)){
            ReadCommentLikePO data = new ReadCommentLikePO();
            data.setUserId(currentUserId);
            data.setCommentId(commonId);
            commentLikeService.save(data);
        }else {
            commentLikeService.remove(wrapper);
        }
        return Res.ok("success");
    }


    @ApiOperation("点赞评论")
    @PostMapping("/likeReply/{replyId}")
    public Res<String> likeReply(@PathVariable Integer replyId){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        QueryWrapper<ReadReplyLikePO> wrapper = new QueryWrapper<ReadReplyLikePO>()
                .eq("reply_id", replyId)
                .eq("user_id", currentUserId);
        ReadReplyLikePO one = replyLikeService.getOne(wrapper);
        if (Objects.isNull(one)) {
            ReadReplyLikePO data = new ReadReplyLikePO();
            data.setReplyId(replyId);
            data.setUserId(currentUserId);
            replyLikeService.save(data);
        }else {
            replyLikeService.remove(wrapper);
        }
        return Res.ok("success");
    }

}
